package com.mallcai.bigdata.ladon.dpl.mapper.system.provider;

import com.mallcai.bigdata.ladon.dpl.entity.system.UserRlRole;
import com.mallcai.bigdata.ladon.dpl.mapper.BaseSqlProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
public class UserRlRoleSqlProvider extends BaseSqlProvider<UserRlRole> {
    public static final String TB_USER_RL_ROLE = "sys_user_rl_role";

    /**
     * 获取表名
     *
     * @return String
     */
    @Override
    public String getTableName() {
        return TB_USER_RL_ROLE;
    }

    /**
     * 获取插入数据的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getInsertColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("user_id", "#{userId}");
        map.put("role_id", "#{roleId}");
        return map;
    }

    /**
     * 获取更新操作的字段列表
     *
     * @return Map
     */
    @Override
    public Map<String, String> getUpdateColumns() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("userId", "user_id");
        map.put("roleId", "role_id");
        return map;
    }

    public String hasRole(@Param("userId") Long userId, @Param("roleCode") String roleCode) {
        return new SQL() {
            {
                SELECT("count(*)");
                FROM(getTableName() + " tb_rl ");
                LEFT_OUTER_JOIN(RoleSqlProvider.TB_ROLE + " tb_ur ON tb_ur.id = tb_rl.role_id ");
                WHERE(" tb_rl.user_id = #{userId} AND tb_ur.`code`  =#{roleCode}");
            }
        }.toString();
    }
}
